import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { BatchMaterialLoadingModel } from '#/api/production/models';

/**
 * 获取表格查询表单的字段配置。
 */
export function useSearchSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
      },
      fieldName: 'barcode',
      label: '物料条码',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<BatchMaterialLoadingModel>,
): VxeTableGridOptions<BatchMaterialLoadingModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'barcode', title: '物料条码', minWidth: 220 },
    { field: 'materialCode', title: '物料编码', width: 120 },
    { field: 'LineCode', title: '产线编号', width: 120 },
    { field: 'operationCode', title: '工序编号', width: 120 },
    { field: 'workstationCode', title: '工位编号', width: 120 },
    { field: 'loadingTime', title: '上料时间', width: 150 },
    {
      field: 'isUnloading',
      title: '已下料',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'red', label: '是', value: true },
          { color: 'green', label: '否', value: false },
        ],
      },
    },
    { field: 'unloadingTime', title: '下料时间', width: 150 },
    {
      cellRender: {
        attrs: {
          nameField: 'barcode',
          nameTitle: '下料',
          onClick: onActionClick,
        },
        props: {
          disabled: (params: any) => params.isUnloading,
        },
        name: 'CellOperation',
        options: [{ code: 'unload', text: '下料', confirm: true }],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 80,
    },
  ];
}
